package com.pingan.stat.service;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.pingan.common.core.property.Properties;
import com.pingan.common.util.ShellExecutor;
import com.pingan.setting.general.entity.GeneralStatSettingBean;
import com.pingan.setting.money24url.entity.Money24UrlBean;

@Component
public class TaskSubmitterService {

	@Properties(name = "referurl.thread")
	private String threadNum;

	@Resource
	private ShellExecutor shellExecutor;

	public void referUrlSubmit(List<Money24UrlBean> beans, final String shell,
			String yyyyMMdd, String output) throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(Integer
				.parseInt(threadNum));
		final CountDownLatch end = new CountDownLatch(beans.size());
		for (Money24UrlBean bean : beans) {
			if (!StringUtils.startsWith(bean.getUrl(), "http://")) {
				bean.setUrl("http://" + bean.getUrl());
			}

			String hql = getReferUrlHQL(yyyyMMdd, bean);

			final String[] params = new String[] { yyyyMMdd, output,
					Integer.valueOf(bean.getId()).toString(),
					"\"" + hql + "\"", };
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					try {
						shellExecutor.excecute(shell, params);
					} catch (Exception ex) {
						ex.getStackTrace();
					} finally {
						end.countDown();
					}
				}
			});
		}

		end.await();
	}

	public void afterUrlSubmit(List<Money24UrlBean> beans, final String shell,
			String yyyyMMdd, String output) throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(Integer
				.parseInt(threadNum));
		final CountDownLatch end = new CountDownLatch(beans.size());
		for (Money24UrlBean bean : beans) {
			if (!StringUtils.startsWith(bean.getUrl(), "http://")) {
				bean.setUrl("http://" + bean.getUrl());
			}

			String hql = getAfterUrlHQL(yyyyMMdd, bean);

			final String[] params = new String[] { yyyyMMdd, output,
					Integer.valueOf(bean.getId()).toString(),
					"\"" + hql + "\"", };
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					try {
						shellExecutor.excecute(shell, params);
					} catch (Exception ex) {
						ex.getStackTrace();
					} finally {
						end.countDown();
					}
				}
			});
		}

		end.await();
	}

	public String getReferUrlHQL(String yyyyMMdd, Money24UrlBean bean) {
		StringBuilder sb = new StringBuilder("select log_date, "
				+ Integer.valueOf(bean.getId()).toString()
				+ ",referurl, count(*) ");

		sb.append(" from " + bean.getSiteId() + ".pv where log_date='"
				+ yyyyMMdd + "' and ");

		if (bean.getMatchType() == 1) {
			sb.append(" url='" + bean.getUrl() + "' group by log_date,"
					+ Integer.valueOf(bean.getId()).toString() + ",referurl");
		} else {
			sb.append(" url like '" + bean.getUrl() + "%' group by log_date,"
					+ Integer.valueOf(bean.getId()).toString() + ",referurl");
		}

		return sb.toString();
	}

	public String getAfterUrlHQL(String yyyyMMdd, Money24UrlBean bean) {
		StringBuilder sb = new StringBuilder("select log_date, "
				+ Integer.valueOf(bean.getId()).toString() + ",url, count(*) ");

		sb.append(" from " + bean.getSiteId() + ".pv where log_date='"
				+ yyyyMMdd + "' and ");

		if (bean.getMatchType() == 1) {
			sb.append(" referurl='" + bean.getUrl() + "' group by log_date,"
					+ Integer.valueOf(bean.getId()).toString() + ",url");
		} else {
			sb.append(" referurl like '" + bean.getUrl()
					+ "%' group by log_date,"
					+ Integer.valueOf(bean.getId()).toString() + ",url");
		}

		return sb.toString();
	}

	public void submit(List<GeneralStatSettingBean> beans, final String shell,
			String yyyyMMdd, String output) throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(beans
				.size());
		final CountDownLatch end = new CountDownLatch(beans.size());
		for (GeneralStatSettingBean bean : beans) {
			final String[] params = new String[] { yyyyMMdd, output,
					bean.getSiteId(), bean.getDomain() };
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					try {
						shellExecutor.excecute(shell, params);
					} catch (Exception ex) {
						ex.getStackTrace();
					} finally {
						end.countDown();
					}
				}
			});
		}

		end.await();
	}

	public void submit(List<GeneralStatSettingBean> beans, final String shell,
			String yyyyMMdd, String output, String time)
			throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(beans
				.size());
		final CountDownLatch end = new CountDownLatch(beans.size());
		for (GeneralStatSettingBean bean : beans) {
			final String[] params = new String[] { yyyyMMdd, output, time,
					bean.getSiteId(), bean.getDomain() };
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					try {
						shellExecutor.excecute(shell, params);
					} catch (Exception ex) {
						ex.getStackTrace();
					} finally {
						end.countDown();
					}
				}
			});
		}

		end.await();
	}
}
